package com.zjuee.web.ledger.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjuee.base.bean.response.R;
import com.zjuee.base.constant.Common;
import com.zjuee.base.dto.common.login.LoginDto;
import com.zjuee.base.dto.common.plan.PlanInfoDto;
import com.zjuee.base.model.common.company.Company;
import com.zjuee.base.valid.Insert;
import com.zjuee.service.PlanService;
import com.zjuee.service.company.service.CompanyService;
import com.zjuee.vo.PlanInfoVo;
import com.zjuee.vo.PlanLoginVo;
import com.zjuee.base.model.common.plan.PlanInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(Common.Service.ZJUEE_CO_MAIN_PLAN_URL_PREFIX)
public class PlanController {

    @Resource
    private PlanService planService;
    @Resource
    private CompanyService companyService;
    @Resource
    private LoginDto loginDto;


    /**
     * 新增计划基本信息 PlanInfo
     *
     * @param planInfo PlanInfo
     * @return Long
     */
    @PostMapping("/info/add")
    public R<Long> addPlanInfo(@Validated(Insert.class) @RequestBody PlanInfo planInfo) {
        try {
            if(planInfo.getWpiYear()==null){
                return R.fail("年份不能为空");
            }
            PlanInfo select = planService.selectInfoByBciId(loginDto.getOrgBusinessId(), planInfo.getWpiYear());
            if(select!=null){
                return R.fail("该年度计划已经存在请勿重复提交");
            }
            Company company = companyService.selectBaseById(loginDto.getOrgBusinessId(), null);

            Long id = null;
            if (company != null) {
                String WpiCode = planService.getWpiCode(company.getBciArea(), planInfo.getWpiYear());
                planInfo.setBciId(loginDto.getOrgBusinessId());
                planInfo.setWpiCode(WpiCode);
                planInfo.setWpiAuditStatus(1);
                planInfo.setWpiIsUpload(0);
                planInfo.setWpiCreator(loginDto.getUserName());
                planInfo.setWpiCreateTime(new Date());
                planInfo.setWpiModifier(loginDto.getUserName());
                planInfo.setWpiModifiedTime(new Date());
                id = planService.add(planInfo);
                return R.ok(id);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail("Resource does not exist");
    }

    /**
     * 查询计划列表
     *
     * @param planInfoDto
     * @return
     */
    @PostMapping("/info/mylist")
    public R<Page<PlanInfoVo>> list(@RequestBody PlanInfoDto planInfoDto) {
        if(loginDto.getOrgBusinessId()==null){
            return R.fail("企业ID不能为空！");
        }
        planInfoDto.setBciId(loginDto.getOrgBusinessId());
        Page<PlanInfoVo> ss = planService.listPagePlanInfo(planInfoDto);
        return R.ok(ss);
    }


    /**
     * 计划查看 - 显示所有的信息
     *
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public R<HashMap<String,Object>> selectById(@NotNull @PathVariable(value = "id")Long id) {
        return R.ok(planService.planAll(id));
    }
    /**
     * 点击提交 - 改变发送计划至监管端
     *
     * @param id
     * @return
     */
    @PostMapping("/commit/{id}")
    public  R<Boolean> commit(@NotNull @PathVariable(value = "id")Long id) {
        return R.ok(planService.commit(id));
    }
    /**
     * 计划导出 - 只导出列表中信息
     *
     * @param planInfo PlanInfo
     */
    @PostMapping("/excel")
    public void excel(@RequestBody PlanInfo planInfo) {

    }

    /**
     * 查询计划基本信息
     *
     * @param planInfo PlanInfo
     * @return List<PlanInfo>
     */
    @GetMapping("/info/base/select")
    public R<List<PlanInfo>> selectPlanInfo(@RequestBody PlanInfo planInfo) {
        return null;
    }


    /**
     * 查询简略信息
     *
     * @param bciId bciId
     * @return PlanLoginVo
     */
    @GetMapping("/info/brief/select")
    public R<PlanLoginVo> selectBriefInfo(@NotNull @RequestParam("bciId") Long bciId,
                                          @NotNull @RequestParam("wpiYear") String wpiYear) {
        try {
            PlanLoginVo select = planService.selectBriefInfo(bciId, wpiYear);
            if (null != select) {
                return R.ok(select);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail("Resource does not exist");
    }

    /**
     * 查询计划主表详细信息
     *
     * @param bciId bciId
     * @return PlanLoginVo
     */
    @GetMapping("/info/brief/selectinfo")
    public R<PlanInfo> selectInfoByBciId(@NotNull @RequestParam("bciId") Long bciId,
                                         @NotNull @RequestParam("wpiYear") String wpiYear) {
        try {
            PlanInfo select = planService.selectInfoByBciId(bciId, wpiYear);
            if (null != select) {
                return R.ok(select);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail("Resource does not exist");
    }

    @PostMapping("/info/getPlanInfo")
    public R<List<PlanInfo>> getPlanInfo(@RequestBody PlanInfo planInfo) {
        return R.ok(planService.getPlanInfo(planInfo));
    }


}
